package com.javafactroy.kunlong.codefactroy.service.domain.valueobj;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.javafactroy.kunlong.codefactroy.consts.CodeFactroyConsts;
import com.javafactroy.kunlong.codefactroy.service.stringtemplate.StringTemplate;
import lombok.Data;

import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
//@Component
public class EntityProps {

    @Transient
    StringTemplate stringTemplate;

    String dbName;
    String clsName;
    String tableName;
    String tableComment;
    String pkey;

    List<EntityProp> propList;

    public EntityProp findPkey() {
        List<EntityProp> lst = propList.stream().filter(i -> i.pkey()).collect(Collectors.toList());
        if (lst.size() == 0) {
            return null;
        }
        return lst.get(0);
    }

    public EntityProps() {
        propList = new ArrayList<>();
    }

    List<String> buildPropLines() {
        List<String> lines = new ArrayList<>();
        propList.forEach(i ->
                lines.add(StrUtil.join("\r\n", i.buildPropLine()))
        );

        return lines;
    }

    List<String> buildPutLines() {
        List<String> lines = new ArrayList<>();
        propList.forEach(i ->
                lines.add(StrUtil.format("\t\tprop2ColMap.put(\"{}\",\"{}\");", i.getPropName(), i.getColName()))
        );

        return lines;
    }

    public void makeParams() {
        makeParams("mysql");
    }

    public String makeEntityProps() {
        return StrUtil.join("\r\n", buildPropLines());
    }

    public String makePutMaps() {
        return StrUtil.join("\r\n", buildPutLines());
    }

    //makeEntityProps
    public void makeParams(String dbtype) {

        if (stringTemplate == null) {

            stringTemplate = new StringTemplate();
        }

        stringTemplate.putParam("dbtype", dbtype);
        stringTemplate.putParam("Date", DateUtil.formatDateTime(new Date()));
        stringTemplate.putParam("Author", CodeFactroyConsts.author);
        stringTemplate.putParam("company", CodeFactroyConsts.company);

        stringTemplate.putParam("clsName", clsName);
        stringTemplate.putParam("tableName", tableName);
        stringTemplate.putParam("tableComment", tableComment == null ? tableName : tableComment);
        stringTemplate.putParam("pkey", pkey);
        stringTemplate.putParam("pkeyProp", StrUtil.toCamelCase(pkey));
        stringTemplate.putParam("PkeyProp", StrUtil.upperFirst(StrUtil.toCamelCase(pkey)));

        stringTemplate.putParam("makeEntityProps", makeEntityProps());
        stringTemplate.putParam("MapPut", makePutMaps());

        makeParamsMapper();

    }

    void makeParamsMapper() {

        stringTemplate.putParam("Base_Column_List", makeBase_Column_List());
        stringTemplate.putParam("BaseResultMap", makeColProps().toCodeString());
        stringTemplate.putParam("Condition_FindByQueryParam", makeCols().toCodeString());

    }


    String makeBase_Column_List() {
        List<String> cols = getPropList().stream().map(i -> i.getColName()).collect(Collectors.toList());
        return "\t\t\t" + StrUtil.join(",", cols) + "\r\n";
    }

    public ColsValueObj makeCols() {
        ColsValueObj colsValueObject = new ColsValueObj();
        propList.forEach(i -> colsValueObject.getColEqValueObjects().add(i.makeCol()));

        return colsValueObject;
    }

    public ColPropsValueObj makeColProps() {
        ColPropsValueObj colsValueObject = new ColPropsValueObj();
        propList.forEach(i -> colsValueObject.getColPropValueObjectList().add(i.makeColProp()));

        return colsValueObject;
    }

}
